package com.yiren.manager.ctrl.system;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrSysMenu;
import com.yiren.entity.YrSysModel;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.system.YrSysMenuForm;
import com.yiren.manager.form.system.YrSysModelForm;
import com.yiren.manager.service.iface.system.YrSysMenuService;
import com.yiren.manager.service.iface.system.YrSysModelService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("sysMenu")
@Controller
public class YrSysMenuController extends GenericController<YrSysMenuForm, YrSysMenu>{

	@Autowired
	private YrSysMenuService selfService;
	
	@Autowired
	private YrSysModelService sysModelService;
	
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrSysMenuForm> findAll() throws Exception{
		HTTPResult<YrSysMenuForm> result = new HTTPResult<YrSysMenuForm>();
		
		String sqlWhere = "parent_id=-1";
		QueryResult<YrSysMenu> qr = selfService.find(YrSysMenu.class, sqlWhere, new Object[]{});
		
		List<YrSysMenuForm> formList = new ArrayList<YrSysMenuForm>();
		for(YrSysMenu entity : qr.getResultList()){
			YrSysMenuForm form = toForm(entity);
			constSysModel(form);
			findChilds(form);
			formList.add(form);
		}
		result.setData(formList);
		
		return result;
	}
	
	public void findChilds(YrSysMenuForm sysMenuForm) throws Exception{
		String sqlWhere = "parent_id=?1";
		QueryResult<YrSysMenu> qr = selfService.find(YrSysMenu.class, sqlWhere, new Object[]{sysMenuForm.getSysMenuId()});
		List<YrSysMenuForm> formList = new ArrayList<YrSysMenuForm>();
		for(YrSysMenu entity : qr.getResultList()){
			YrSysMenuForm form = toForm(entity);
			constSysModel(form);
			findChilds(form);
			formList.add(form);
		}
		sysMenuForm.setChildren(formList);
	}
	
	public void constSysModel(YrSysMenuForm sysMenuForm) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		String sqlWhere = "sys_menu_id=?1";
		List<YrSysModelForm> formList = new ArrayList<YrSysModelForm>();
		QueryResult<YrSysModel> qr  = sysModelService.find(YrSysModel.class, sqlWhere, new Object[]{sysMenuForm.getSysMenuId()});
		for(YrSysModel sysModel : qr.getResultList()){
			formList.add(ObjectUtils.objectToObject(sysModel, YrSysModelForm.class));
		}
		sysMenuForm.setSysModelForms(formList);
	}
	
	@Override
	public YrSysMenuForm toForm(YrSysMenu e) throws Exception {
		YrSysMenuForm form = ObjectUtils.objectToObject(e, YrSysMenuForm.class);
		return form;
	}

	@Override
	public YrSysMenu toEntity(YrSysMenuForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
